home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / DEMON / RISCOS2 / TCP_131S.ARC / c / TRACE < prev    next >
Text File  |  1994-03-03  |  4KB  |  165 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <time.h>
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "iface.h"
  9. #include "trace.h"
  10. #include "session.h"
  11. #include "misc.h"
  12.  
  13. static void ctohex(char *, int16);
  14.  
  15. /* Redefined here so that programs calling dump in the library won't pull
  16.  * in the rest of the package
  17.  */
  18. static char nospace[] = "No space!!\n";
  19.  
  20. void dump(register struct interface *interface, int direction,
  21.           unsigned type, struct mbuf *bp)
  22. {
  23.         struct mbuf *tbp;
  24.         void (*func)();
  25.         int16 size;
  26.         time_t t;
  27.         struct tm *tm;
  28.  
  29.         if((interface->trace & direction) == 0)
  30.                 return; /* Nothing to trace */
  31.  
  32.         time(&t);
  33.         tm = localtime(&t);
  34.  
  35.         switch(direction){
  36.         case IF_TRACE_IN:
  37.                 twprintf("%02d:%02d:%02d %s recv:\n",tm->tm_hour,tm->tm_min,tm->tm_sec,interface->name);
  38.                 break;
  39.         case IF_TRACE_OUT:
  40.                 twprintf("%02d:%02d:%02d %s sent:\n",tm->tm_hour,tm->tm_min,tm->tm_sec,interface->name);
  41.                 break;
  42.         }
  43.         if(bp == NULLBUF || (size = len_mbuf(bp)) == 0){
  44.                 twprintf("empty packet!!\n");
  45.                 return;
  46.         }
  47.  
  48.         if(type < NTRACE)
  49.                 func = tracef[type];
  50.         else
  51.                 func = NULLVFP;
  52.  
  53.         dup_p(&tbp,bp,0,size);
  54.         if(tbp == NULLBUF){
  55.                 twprintf(nospace);
  56.                 return;
  57.         }
  58.         if(func != NULLVFP)
  59.                 (*func)(&tbp,1);
  60.         if(interface->trace & IF_TRACE_ASCII){
  61.                 /* Dump only data portion of packet in ascii */
  62.                 ascii_dump(&tbp);
  63.         } else if(interface->trace & IF_TRACE_HEX){
  64.                 /* Dump entire packet in hex/ascii */
  65.                 free_p(tbp);
  66.                 dup_p(&tbp,bp,0,len_mbuf(bp));
  67.                 if(tbp != NULLBUF)
  68.                         hex_dump(&tbp);
  69.                 else
  70.                         twprintf(nospace);
  71.         }
  72.         free_p(tbp);
  73. }
  74.  
  75. /* Dump an mbuf in hex */
  76. void hex_dump(register struct mbuf **bpp)
  77. {
  78.         int16 n;
  79.         int16 address;
  80.         char buf[16];
  81.  
  82.         if(bpp == NULLBUFP || *bpp == NULLBUF)
  83.                 return;
  84.  
  85.         address = 0;
  86.         while((n = pullup(bpp,buf,sizeof(buf))) != 0){
  87.                 fmtline(address,buf,n);
  88.                 address += n;
  89.         }
  90. }
  91. /* Dump an mbuf in ascii */
  92. void ascii_dump(register struct mbuf **bpp)
  93. {
  94.         char Buffer[10];
  95.         char c;
  96.         register int16 tot;
  97.         char *line;
  98.  
  99.         if(bpp == NULLBUFP || *bpp == NULLBUF)
  100.                 return;
  101.  
  102.         line = malloc(2000);
  103.         *line = '\0';
  104.         tot = 0;
  105.  
  106.         while(pullone(bpp,&c) == 1)
  107.         {
  108.                 if((tot % 64) == 0)
  109.                 {
  110.                         sprintf(Buffer, "%04x  ",tot);
  111.                         strcat(line, Buffer);
  112.                 }
  113.                 if (isprint(c)) {
  114.                         sprintf(Buffer, "%c", c);
  115.                         strcat(line, Buffer);
  116.                 } else {
  117.                         strcat(line, ".");
  118.                 }
  119.                 if((++tot % 64) == 0)
  120.                         strcat(line, "\n");
  121.         }
  122.         if((tot % 64) != 0)
  123.                 strcat(line, "\n");
  124.  
  125.         twputs(line);
  126.         free(line);
  127. }
  128. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  129.  * translation, e.g.,
  130.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  131.  */
  132. void fmtline(int16 addr, char *buf, int16 len)
  133. {
  134.         char line[80];
  135.         register char *aptr,*cptr;
  136.         register char c;
  137.  
  138.         memset(line,' ',sizeof(line));
  139.         ctohex(line,hibyte(addr));
  140.         ctohex(line+2,lobyte(addr));
  141.         aptr = &line[6];
  142.         cptr = &line[55];
  143.         while(len-- != 0){
  144.                 c = *buf++;
  145.                 ctohex(aptr,(int16)uchar(c));
  146.                 aptr += 3;
  147.                 c &= 0x7f;
  148.                 *cptr++ = isprint(c) ? c : '.';
  149.         }
  150.         *cptr++ = '\r';
  151.         *cptr++ = '\n';
  152.         *cptr   = '\0';
  153.  
  154.         twputs(line);
  155. }
  156. /* Convert byte to two ascii-hex characters */
  157. static void ctohex(register char *buf, register int16 c)
  158. {
  159.         static char hex[] = "0123456789abcdef";
  160.  
  161.         *buf++ = hex[hinibble(c)];
  162.         *buf = hex[lonibble(c)];
  163. }
  164.  
  165.